package com.gisuni.sddc.platformManager.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 请求的响应
 * 
 * @author yuhao
 * 
 */
public class Response {

	private boolean success;

	private int total;

	private Object result;
	
	private String msg;

	public Response() {
		this.result = new JSONObject();
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public static JSONObject toJSONObject(Response result) throws JSONException {
		JSONObject jsonObj = new JSONObject(result);
		String t = result.getResult()==null?"{}":result.getResult().toString();
		
		if (t.startsWith("["))
		{
			jsonObj.put("rows", new JSONArray(t));
			jsonObj.put("total",result.getTotal());
		}
		else if (t.startsWith("{"))
			jsonObj.put("rows", new JSONObject(t));
		return jsonObj;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
